﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Linq;
using System.Collections.Generic;
using Base.Custom;
using Base.Extensions;
public class FreeCamera : MonoBehaviour
{
    private static FreeCamera _freecamera;
    public static FreeCamera freecamera
    {
        get
        {
            if (_freecamera == null)
            {
                _freecamera = GameObject.Find("Main Camera").GetComponent<FreeCamera>();
            }
            return _freecamera;
        }
        set
        {
            _freecamera = value;
        }
    }
    public float zoomSpeed;
    public float dragSpeed;
    public float moveSpeed;
    public float scaleSpeed;
    public GameObject path;
    public SingleObject singleobj;
    public bool 多选=false;
    public static Stack<SingleObject> 关系集合;
    private void Awake()
    {
        关系集合 = new Stack<SingleObject>();
    }


    private void Update()
    {
            #region 初始化镜头拉近动画
       
            #endregion
            #region 判断是否在UI层
        if (IsTouchedUI())
            return;
            #endregion
            #region 鼠标拖动镜头操作
            if (Input.GetMouseButton(1))
            {
                float CameraX = Input.GetAxis("Mouse X");
                float CameraY = Input.GetAxis("Mouse Y");
                Vector3 Angle = new Vector3(CameraY * 2, -CameraX * 2, 0);
                this.transform.eulerAngles -= Angle;
            }
            else if (Input.GetMouseButton(2))
            {
                transform.Translate(Vector3.left * Input.GetAxis("Mouse X") * dragSpeed);
                transform.Translate(Vector3.up * Input.GetAxis("Mouse Y") * -dragSpeed);
            }
            if (Input.GetAxis("Mouse ScrollWheel") != 0/*&&Vector3.Distance(freecamera.transform.position,GameObject.Find("Sky Dome").transform.position)<1000*/)
            {
                //print(Input.GetAxis("Mouse ScrollWheel"));
                transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel")) * zoomSpeed);
            }
            #endregion
            #region 按钮控制镜头
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(new Vector3(0, 0, 1) * Time.deltaTime * moveSpeed);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(new Vector3(0, 0, -1) * Time.deltaTime * moveSpeed);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.Translate(new Vector3(-1, 0, 0) * Time.deltaTime * moveSpeed);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime * moveSpeed);
            }
        #endregion
        #region 鼠标选中

        if (Input.GetKeyDown(KeyCode.LeftShift))
            多选 = true;
        if (Input.GetKeyUp(KeyCode.LeftShift))
            多选 = false;
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000f, LayerMask.GetMask("Mode")))
            {
                if (singleobj != null&& !多选)
                    singleobj.SetHighlighter();
                singleobj = hit.transform.GetComponent<SingleObject>();
                singleobj.SetHighlighter(Color.yellow);
                if (多选)
                {
                    if (!关系集合.Contains(singleobj))
                    {
                        关系集合.Push(singleobj);
                    }
                }
            }
            else
            {
                if (singleobj != null)
                    singleobj.SetHighlighter();
                if (关系集合.Count!=0)
                {
                    for (int i = 关系集合.Count; i >0; i--)
                    {
                        SingleObject obj = 关系集合.Pop();
                        obj.SetHighlighter();
                    }
                }
                singleobj = null;
            }
        }
        if (Input.GetMouseButtonDown(1))
        {
            if (singleobj!=null)
            {
                if (true/*空间选择模式开打*/)
                {
                    /*打开右键=>空间右键 */
                }
                else
                {
                    /*打开右键=>属性右键 */
                }

            }
            else
            {
                UIManager.Instance.PopPanel();
            }
        }

        #endregion
    }
    /// <summary>
    /// 鼠标是否在UI上检查
    /// </summary>
    /// <returns></returns>
    public bool IsTouchedUI()
    {
        bool touchedUI = false;
        if (UnityEngine.Application.isMobilePlatform)
        {
            if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                touchedUI = true;
            }
        }
        else if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            touchedUI = true;
        }
        return touchedUI;
    }
    /// <summary>
    /// 重置摄像头
    /// </summary>
    public void ResetCamera()
    {
      
    }

    /// <summary>
    /// 高亮、描边、正常状态
    /// </summary>
    /// <param name="id">设置对象id</param>
    /// <param name="state">状态-1正常，0屏幕高亮，1物体描边</param>
    public void FindObjcet(string id,int state=0)
    {

    }
}








       


